{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组索引的操作",
   "id": "582dba34f35bd352"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T01:57:30.629981Z",
     "start_time": "2025-08-26T01:57:30.494684Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "943906da1318d29e",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 1. 数组对象的切片\n",
    "- 数组对象切片的参数设置与列表切面参数类似\n",
    "    - 步长+：默认切从首到尾\n",
    "    - 步长-：默认切从尾到首\n",
    "- 语法：数组对象[起始位置:终止位置:步长, ...]\n",
    "- 默认位置步长：1\n"
   ],
   "id": "b0ca190f44479ba0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T02:10:29.547880Z",
     "start_time": "2025-08-26T02:10:29.533564Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "数组切片(规则和Python中列表一样)\n",
    "\"\"\"\n",
    "arr1 = np.arange(1,10) # 创建包含9个元素的数组 1-9\n",
    "print(arr1) # [1 2 3 4 5 6 7 8 9]\n",
    "\n",
    "print(arr1[:3])  # 起始位置不写默认从0开始，步长不写，默认是1 [1 2 3]\n",
    "\n",
    "print(arr1[3:6]) # [4 5 6]\n",
    "\n",
    "print(arr1[6:]) # [7 8 9]\n",
    "\n",
    "print(arr1[::2]) # [1 3 5 7 9]\n",
    "\n",
    "print('=' * 30)\n",
    "\n",
    "print(arr1[::-1])  # [9 8 7 6 5 4 3 2 1]\n",
    "print(arr1[:-4:-1]) # [9 8 7] # 从倒数第一个倒序到倒数第四个，不包含第四个\n",
    "print(arr1[-7::-1]) # [3 2 1]"
   ],
   "id": "797f5ba5d2121ff0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7 8 9]\n",
      "[1 2 3]\n",
      "[4 5 6]\n",
      "[7 8 9]\n",
      "[1 3 5 7 9]\n",
      "==============================\n",
      "[9 8 7 6 5 4 3 2 1]\n",
      "[9 8 7]\n",
      "[3 2 1]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T02:32:49.803106Z",
     "start_time": "2025-08-26T02:32:49.793183Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 多维数组的切片\n",
    "arr2 = np.arange(1,28) # 生成1到27之间的27个元素的数组\n",
    "# arr2 = arr2.reshape(3,3,3)  # 改变数组的形状 返回一个新的数组\n",
    "arr2.resize(3,3,3) # 直接改变数组的形状\n",
    "print(arr2)\n",
    "\n",
    "print(\"=\" * 30)\n",
    "\n",
    "print(arr2[1::]) # 从第二页开始取出到结尾 第二页，第三页\n",
    "\n",
    "print('*' * 30)\n",
    "print(arr2[1,::]) # 在页的维度上取第二个元素，第二页中行和列中的所有元素\n",
    "print('=' * 30)\n",
    "print(arr2[1,:1, :]) # # 在页的维度上取第二个元素，在第二页中行的维度上取第二个元素，列中所有元素\n",
    "print('/' * 30)\n",
    "print(arr2[1,:,1:]) # 在页的维度上取第二个元素，在第二页中行的维度上取所有元素，列中取第二列到最后的元素\n",
    "\n",
    "print('$' * 30)\n",
    "print(arr2[0,:,1]) # 第一页 所有行，第二列 [2 5 8]\n",
    "\n",
    "\n"
   ],
   "id": "641f6907994350c6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 1  2  3]\n",
      "  [ 4  5  6]\n",
      "  [ 7  8  9]]\n",
      "\n",
      " [[10 11 12]\n",
      "  [13 14 15]\n",
      "  [16 17 18]]\n",
      "\n",
      " [[19 20 21]\n",
      "  [22 23 24]\n",
      "  [25 26 27]]]\n",
      "==============================\n",
      "[[[10 11 12]\n",
      "  [13 14 15]\n",
      "  [16 17 18]]\n",
      "\n",
      " [[19 20 21]\n",
      "  [22 23 24]\n",
      "  [25 26 27]]]\n",
      "******************************\n",
      "[[10 11 12]\n",
      " [13 14 15]\n",
      " [16 17 18]]\n",
      "==============================\n",
      "[[10 11 12]]\n",
      "//////////////////////////////\n",
      "[[11 12]\n",
      " [14 15]\n",
      " [17 18]]\n",
      "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n",
      "[2 5 8]\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. ndarray数组的掩码操作",
   "id": "40c5de43725f4cbc"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T02:51:04.184966Z",
     "start_time": "2025-08-26T02:51:04.171159Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 掩码操作：理解成一个筛子\n",
    "# 把True对应位置上的元素保留下来，False对应位置上的元素丢弃\n",
    "arr3 = np.arange(1,6) # 创建一个包含5个元素的一维数组\n",
    "print(arr3)\n",
    "\n",
    "mask = [True,False,True,False,False]\n",
    "\n",
    "print(arr3[mask]) # 进行掩码操作\n",
    "\n",
    "mask = (arr3 > 3)\n",
    "print(mask)\n",
    "print(arr3[mask])\n",
    "\n",
    "print(arr3[arr3 > 3]) # 执行掩码操作，将符合条件的元素筛选出来"
   ],
   "id": "148143c8a2ba0e3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "[1 3]\n",
      "[False False False  True  True]\n",
      "[4 5]\n",
      "[4 5]\n"
     ]
    }
   ],
   "execution_count": 43
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
